package cn.broadsense.thrid.api;

import cn.broadsense.thrid.constant.DaHuaApiUrlConstant;
import cn.broadsense.thrid.param.*;
import cn.broadsense.thrid.result.*;
import cn.broadsense.thrid.utils.DaHuaHttpUtils;
import cn.hutool.core.util.URLUtil;
import com.alibaba.fastjson2.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 特征检索 视频相关
 *
 * @author pengxin
 * @date 2022-10-11 14:43
 */
@Slf4j
@Component
public class VideoExRepository extends BaseRepository {


    /**
     * 以图搜图
     *
     * @param param
     * @return
     */
    @Deprecated
    public VideoExTractListResult personFace(PersonFaceParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VIDEOEX_PERSON_FACE);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 以图搜图新接口
     *
     * @param param
     * @return
     */
    public VideoExTractListResult videoExTractList(VideoExTractListParam param) {
        try {
            param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.NEW_VIDEOEX_PERSON_FACE);
            return DaHuaHttpUtils.postRest(getCToken(), param);
        } catch (Exception e) {
            log.error("获取接口数据失败：{}", e.getMessage());
            return new VideoExTractListResult();
        }
    }

    /**
     * 搜索图片中的物体 抠图
     *
     * @return
     */
    public VideoExtractResult videoExtract(VideoExtractParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VIDEOEX_EXTRACT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 搜索图片中的物体 人像
     *
     * @return
     */
    public List<FaceDetectResult> faceDetectExtract(FaceDetectParam param) {
        List<FaceDetectResult> list = new ArrayList<>();
        param.setUrl(cUrl + DaHuaApiUrlConstant.PeopleAi.FACE_DETECT);
        try {
            list = DaHuaHttpUtils.postRest(getCToken(), param);
        } catch (Exception e) {
            log.error("检测图片失败，失败信息：{}", e.getMessage());
        }
        return list;
    }


    /**
     * 车辆以图搜图任务
     *
     * @param param
     * @return
     */
    public VideoExTractListResult vehicleImageSearchTask(VehicleImageTaskParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VEHICLE_IMAGE_SEARCH_TASK);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 车辆以图搜图进度
     *
     * @param param
     * @return
     */
    public VideoExTractListResult vehicleImageSearchProgress(VehicleImageTaskParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VEHICLE_IMAGE_SEARCH_PROGRESS);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 车辆以图搜图结果
     *
     * @param param
     * @return
     */
    public VideoExTractListResult vehicleImageSearchResult(VehicleImageSearchParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VEHICLE_IMAGE_SEARCH_RESULT);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }

    /**
     * 特征检索列表，查询特征检索的数据
     *
     * @param param
     * @return
     */
    public FtsListResult ftsList(FtsListParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VIDEOEX_FTS_SEARCH);
        return DaHuaHttpUtils.postRest(getCToken(), param);
    }


    /**
     * 获取特征检索总数
     *
     * @param param
     * @return
     */
    public Integer ftsTotal(FtsTotalsParam param) {
        param.setUrl(cUrl + DaHuaApiUrlConstant.VideoFeatures.VIDEOEX_FTS_TOTALS);
        JSONObject result = DaHuaHttpUtils.postRest(getCToken(), param);
        if (result.containsKey("total")) {
            return result.getInteger("total");
        }
        log.error("FtsRepository：获取特征总数失败");
        return 0;
    }

    /**
     * 特征检索详情
     *
     * @param param
     * @return
     */
    public FtsDetailsResult ftsDetails(FtsDetailsParam param) {
        param.checkParam();
        String url = cUrl + String.format(DaHuaApiUrlConstant.VideoFeatures.VIDEOEX_FTS_DETAIL, param.getType(), param.getRecordId());
        Map<String, Object> paramMap = new HashMap<>();
        paramMap.put("capTime", param.getCapTime());
        paramMap.put("searchType", param.getSearchType());
        String query = URLUtil.buildQuery(paramMap, null);
        param.setUrl(url + "?" + query);
        FtsDetailsResult rest = DaHuaHttpUtils.getRest(getCToken(), param);
        if (rest != null) {
            rest.setFaceImgUrl(rest.getFaceImgUri());
        }
        return rest;
    }


}
